<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * HITI AdminCP
 *
 * @package     HITI AdminCP Project
 * @since       Monday, October 21, 2013, 6:00 PM
 * @final       
 * @category    Models
 * @see 		Product properties
 * @author      PHUOCNGUYEN - HITI Dev Team
 * @copyright   Copyright (c) 2013, HITI Corp
 */

class Product_properties_model extends CI_Model {
	public function __construct()
	{
		parent::__construct();
	}

	public function show($cat, $uri)
	{
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->where('p.cat', $cat);
		if ($uri['q'] != '')
		{
			$this->db->like('p.name_vn', $uri['q']);
			$this->db->or_like('p.name_en', $uri['q']);
		}
		$last_login = $GLOBALS['user']['last_login'];
		$this->db->from('product_properties AS p');
		$this->db->select('p.id, p.cat, p.name_vn, p.name_en, p.active, p.deleted, IF ('.sprintf('p.date_added >= %1$s', $this->db->escape($last_login)).', 1, 0) as isnew', false);
		$this->db->select('(SELECT COUNT(id) FROM products WHERE ('.sprintf('%s LIKE CONCAT("%%\"", p.id, "\"%%")) OR %s = p.id', $cat, $cat).') AS product', false);
		$this->db->order_by('p.sort_order DESC');
		$this->db->where('p.deleted', $uri['deleted']);
		$query = $this->db->get();
		if ($query->num_rows() > 0)
		{
			return $query->result_array();
		}
		else
		{
			return false;
		}
	}

	public function info($id)
	{
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->where('id', $id);
		$query = $this->db->get('product_properties');
		if ($query->num_rows() > 0)
		{
			return $query->row_array();
		}
		else
		{
			$fields = $this->db->field_data('product_properties');
			$default_data = array();
			foreach ($fields as $field)
			{
				$default_data[$field->name] = $field->default;
			}
			return $default_data;
		}
	}


	public function process($data, $id = '')
	{
		if(!is_array($data))
		{
			return false;
		}
		$this->db->query('SET NAMES "LATIN1"');
		if ($id > 0)
		{
			unset($data['date_added']);
			$this->db->where('id', $id);
			$this->db->update('product_properties', $data);
			write_log('update_product_properties', $data['name_vn']);
		}
		else
		{
			$this->db->insert('product_properties', $data);
			write_log('add_product_properties', $data['name_vn']);
		}
		return true;
	}
}

/* End of file product_properties_model.php */
/* Location: ./application/models/product_properties_model.php */